package com.haopt.quartz.config;

import com.haopt.quartz.demo.MyJob;
import com.haopt.quartz.demo.MyJob2;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author haopt
 * @version 1.0
 * @ClassName MainConfig
 * @Description TODO
 * @date 2021/5/10 12:05
 */

@Configuration
public class MainConfig {

    /**
     * 创建JobDetail，交由spring管理
     */
    @Bean
    public JobDetail jobDetail() {
        return JobBuilder.
                newJob(MyJob.class).
                withIdentity("myJob", "group1").
                storeDurably().
                build();
    }

    @Bean
    public Trigger trigger(@Qualifier("jobDetail") JobDetail jobDetail) {
        return TriggerBuilder.newTrigger().
                withIdentity("trigger1","group1").
                startNow().
                withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * * * ?")).
                forJob(jobDetail).
                build();
    }

    @Bean
    public JobDetail jobDetail2() {
        return JobBuilder.
                newJob(MyJob2.class).
                withIdentity("myJob2", "group1").
                storeDurably().
                build();
    }
    @Bean
    public Trigger trigger2(JobDetail jobDetail2) {
        return TriggerBuilder.newTrigger().
                withIdentity("trigger2","group1").
                startNow().
                withSchedule(CronScheduleBuilder.cronSchedule("0/6 * * * * ?")).
                forJob(jobDetail2).
                build();
    }

}
